$(function(){
    /**
     * 分页初始条件
     */
    var total_count = 1;//分页总记录数
    var total = 1;//分页总页面数
    var currentPage = 1;//当前页
    var pageSize = pageOption.pageSize;//每页显示的记录数
    var idList = [];//批量选择id所存的数组
    var root_url = $("#root_url").val();

    /**
     * 页面初始化
     */
    function init(){
        myPagination();
        bindEvent();
    }

    /**
     * 事件绑定
     */
    function bindEvent(){
        //根据条件查询订单信息
        $('.inner-section #search_btn').click(function() {
            render(true,1,pageSize);
            return false;
        });

        //enter键盘事件
        $(".inner-section .search-param-form input").keydown(function(event){
            event = event ? event: window.event;
            if(event.keyCode == 13){
                render(true,1,pageSize);
                return false;
            }
        });
    }

    /**
     * 获取模糊参数
     */
    function getSelectInfo(){
        var selectInfo = {};
        $.each($('#search-param-form').serializeArray(), function(i, field){
            selectInfo[field.name]=field.value;
        });
        JSON.stringify(selectInfo);
        return selectInfo;
    }
    /**
     * 分页显示方法
     */
    function myPagination(){
        render(true,1,pageSize);
        //调用公共分页方法
        pagination("#page-selection",{pageSize:pageSize,total:total},render);

    }
    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async,pageIndex,pageSize){
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async:async,
            type:'post',
            url:'pagingBalance',
            data:selectInfo,//从1开始计数
            dataType:'json',
            success:function(result){
                var html ='';
                if(result.errorCode == 0){
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total:total,total_count:total_count});//重新计算总页数,总记录数

                    currentPage = result.data.pageInfo.current_page;
                    var myList = result.data.dataList;

                    html+='<tr>' +
                        '<th class="th1">序号</th>' +
                        '<th class="">订单号</th>' +
                        '<th class="">员工</th>' +
                        '<th class="" style="width: 400px">说明</th>' +
                        '<th class="">金额</th>' +
                        '<th class="">状态</th>' +
                        '<th class="">时间</th>' +
                        '</tr>';
                    var colspan = $(html).find("th").length;
                    for(var i = 0; i < myList.length;i++){
                        var obj = myList[i];
                        var num = (pageIndex-1)*pageSize + i+1;
                        var type = obj.type; //类型
                        var amount = obj.amount; //金额
                        var add_time = obj.add_time; //时间
                        var account = obj.account; //用户
                        var describe = obj.describe; //用户
                        var order = obj.order_info;
                        var id = obj.id;
                        var checked = (idList.indexOf(id) >= 0) ? "checked":"";//判断当前记录先前有没有被选中

                        var zj_state_text = '<span style="color: green">可提现</span>';
                        if (obj.zj_state == 1) zj_state_text = '<span style="color: #0a8ddf">冻结中</span>'
                        if (obj.zj_state == 2) zj_state_text = '<span style="color: grey">已失效</span>'

                        html+='<tr>'
                            +'<td class="th1">'+num+'</td>'
                            +'<td class="th2"><a href="javascript:;">'+order.code+'</a></td>'
                            +'<td class="th2">'+account+'</td>'
                            +'<td class="th2">'+describe+'</td>'
                            +'<td class="th2"><span style="color: red">'+amount+'</span></td>'
                            +'<td class="th2">'+zj_state_text+'</td>'
                            +'<td class="th2">'+add_time+'</td>'
                            +'</tr>';
                    }

                    if(myList.length == 0){
                        html += '<tr><td colspan="'+colspan+'"><p class="text-danger">暂无数据。</p></td></tr>';
                        $("#list-table tbody").html(html);
                    }else{
                        $("#list-table tbody").html(html);
                        myCheck();
                        batchSelect(idList,".inner-section #list-table .select-all",".inner-section #list-table .select-single");
                    }

                }else{
                    responseTip(result.errorCode,result.errorInfo,1500);
                }
            },
            error:errorResponse
        });
    }
    
    init();
});